<template>
  <div class="app-container">
    <h4>快递员管理</h4>
    <el-form ref="form" class="el_one" :model="formInline">
      <el-row>
        <el-col :span="8">
          <div class="grid-content">
            <el-form-item label="快递员账号:" prop="account">
              <el-input v-model="formInline.account" placeholder="请输入快递员账号" style="width: 250px;" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <el-form-item label="快递员姓名:" prop="name">
              <el-input v-model="formInline.name" placeholder="请输入快递员姓名" style="width: 250px;" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <el-form-item label="快递员手机号:" prop="phone">
              <el-input v-model="formInline.phone" placeholder="请输入快递员手机号" style="width: 250px;" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="institution-content">
            <el-form-item label="所属地区:">
              <el-select ref="select" v-model="region" placeholder="请选择所属机构" style="width: 250px;">
                <el-option
                  value=""
                  style="display:none"
                />
                <el-tree
                  :data="dataTerr"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  @node-click="handleNodeClick"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="buttod-content" style="padding-left: 30px; padding-right: 30px;">
            <el-form-item>
              <el-button type="danger" style="width: 90px" @click="search">搜索</el-button>
              <el-button style="width: 90px" @click="fnReset">重置</el-button>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div class="el-card__body">
      <el-table type="index" stripe style="width: 100%" :data="list">
        <el-table-column border type="index" label="序号" width="100" />
        <el-table-column prop="account" label="快递员账号" width="200" />
        <el-table-column prop="name" label="快递员姓名" width="300" />
        <el-table-column prop="mobile" label="快递员手机号" width="250" />
        <el-table-column prop="agency.name" label="所属机构" width="300" />
        <el-table-column prop="userId" label="邮编" width="230" />
        <el-table-column fixed="right" label="操作" width="120">
          <template v-slot="{row}">
            <el-button type="text" size="small" @click="fnEdit(row)">作业分配范围</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="formInline.pageSize"
          :current-page="formInline.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="+counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getExpress, getTree } from '@/api/staff'
export default {
  data() {
    return {
      dataTerr: [],
      defaultProps: {
        children: 'children', // 控制树形的子集
        label: 'name' // 控制树形节点显示的文字
      },
      formInline: {
        account: '', // 账号
        name: '', // 姓名
        phone: '', // 电话号
        agentId: '', // 机构id
        page: 1, // 当前页码
        pageSize: 10 // 每页条数
      },
      region: '',
      list: [], // 数据源
      counts: 0 // 数据总条数
    }
  },
  created() {
    // 创建staff.js 请求模块.封装获取方法
    // 调用
    this.initData()
    this.getTreeData()
  },
  methods: {
    fnEdit(row) {
      // console.log(row)
      this.$router.push({
        path: '/user/mapcontent',
        query: {
          rowData: row
        }
      })
    },
    async getTreeData() {
      const result = await getTree()
      console.log(JSON.parse(result))
      this.dataTerr = JSON.parse(result)
    },
    async initData() {
      const result = await getExpress({
        page: this.formInline.page,
        pageSize: this.formInline.pageSize
      })
      console.log(result)// result.items 当前页的数据
      this.list = result.items
      this.counts = result.counts // 总条数
    },
    // 每页条数切换得处理函数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.formInline.pageSize = val
      this.initData()
    },
    // 当前页码处理的函数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)// val:切换的当前页码
      // 修改当前页码,重新发起请求
      this.formInline.page = val
      this.initData()
    },
    // 搜索
    async search() {
      const res = await getExpress(this.formInline)
      console.log(res)
      this.list = res.items
      this.counts = res.counts
    },
    // 重置
    fnReset() { // resetFields()对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.form.resetFields()
      this.region = ''
      this.formInline.page = 1
      this.initData()
    },
    // 点击树形
    handleNodeClick(data) {
      this.region = data.name
      this.formInline.agentId = data.id
      this.$refs.select.blur()
    }
  }

}

</script>

<style lang="scss">
.el_one{
  width: 100%;
  height: 150px;
  background-color: #fff;
}
.grid-content{
  margin-left: 40px;
  margin-top: 20px;
}
.institution-content{
  margin-left: 55px;
}
.el-card__body{
  width: 100%;
  background-color: #fff;
  margin-top: 20px;
  padding:28px 28px 28px 28px;
}
.block{
  margin-left: 330px;
  margin-top: 20px;
}
</style>
